﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfDrawing
{
    /// <summary>
    /// WriteableBitmaptmp.xaml 的交互逻辑
    /// </summary>
    public partial class WriteableBitmaptmp : UserControl
    {
        int width;
        int height;
        WriteableBitmap wBitmap;
        public WriteableBitmaptmp()
        {
            InitializeComponent();


        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            wBitmap.Lock();
            Bitmap backBitmap = new Bitmap(width, height, wBitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, wBitmap.BackBuffer);

            Graphics graphics = Graphics.FromImage(backBitmap);
            graphics.Clear(System.Drawing.Color.White);//整张画布置为白色

            //画一些随机线
            Random rand = new Random();
            for (int i = 0; i < 100; i++)
            {
                int x1 = rand.Next(width);
                int x2 = rand.Next(width);
                int y1 = rand.Next(height);
                int y2 = rand.Next(height);
                graphics.DrawLine(Pens.Red, x1, y1, x2, y2);
            }

            graphics.Flush();
            graphics.Dispose();
            graphics = null;

            backBitmap.Dispose();
            backBitmap = null;

            wBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
            wBitmap.Unlock();
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            
                unsafe
                {
                    var bytes = (byte*)wBitmap.BackBuffer.ToPointer();
                    
                    wBitmap.Lock();

                    //整张画布置为白色
                    for (int i = wBitmap.BackBufferStride * wBitmap.PixelHeight - 1; i >= 0; i--)
                    {
                        bytes[i] = 255;
                    }

                    //画一些随机的红点
                    Random rand = new Random();
                    for (int i = 0; i < 10000; i++)
                    {
                        int x = rand.Next(width);
                        int y = rand.Next(height);
                        int array_start = y * wBitmap.BackBufferStride + x * 3;

                        bytes[array_start] = 0;
                        bytes[array_start + 1] = 0;
                        bytes[array_start + 2] = 255;
                    }

                    wBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
                    wBitmap.Unlock();
            }
                    

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            width = (int)OutCanvas.ActualWidth;
            height = (int)OutCanvas.ActualHeight;
            if (width > 0 && height > 0)
            {
                DisplayImage.Width = width;
                DisplayImage.Height = height;

                wBitmap = new WriteableBitmap(width, height, 72, 72, PixelFormats.Bgr24, null);

                int stride = wBitmap.PixelWidth * wBitmap.Format.BitsPerPixel / 8;

                //跟新一个20*20pixel 的区域,所需的字节长度位
                int bytelenth = 20 * 20 * wBitmap.Format.BitsPerPixel / 8;

                Int32Rect rect = new Int32Rect(100, 100, 20, 20);

                byte[] colordata = new byte[bytelenth];
                for(int i=0;i<bytelenth;i++)
                {
                    colordata[i] = 180;
                }

                wBitmap.WritePixels(rect, colordata, 60, 0);

                DisplayImage.Source = wBitmap;

                int j = 100; int k = 100;
                Task.Run(() =>
                {                   
                    while (true)
                    {
                        rect = new Int32Rect(j++, k++, 20, 20);
                        Dispatcher.Invoke(() =>
                        {
                            wBitmap.WritePixels(rect, colordata, 60, 0);
                        });
                        Thread.Sleep(20);
                    }


                });
            }



        }
    }
}
